package com.mumu.jetpackdemo.livedata;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.mumu.jetpackdemo.R;

/**
 * - @Description: liveData使用<br/>
 * - @Author: edie<br/>
 * - @Time: 2019/3/27 上午11:19
 */
public class DemoLiveDataActivity extends AppCompatActivity implements View.OnClickListener {
    private Toolbar toolbar;
    private TextView helloTv;
    private My1ViewModel mViewModel;

    public static void startLiveData(Context context) {
        context.startActivity(new Intent(context, DemoLiveDataActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_live_data);
        initView();
        mViewModel = ViewModelProviders.of(this).get(My1ViewModel.class);
//        mViewModel.getLiveData().observe(this, new Observer<String>() {
//            @Override
//            public void onChanged(@Nullable String s) {
//                helloTv.setText(s);
//            }
//        });

        mViewModel.getTran1().observe(this, person -> helloTv.setText("年龄:" + person.mAge + " 名字" + person.mName));
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        helloTv = (TextView) findViewById(R.id.hello_tv);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        findViewById(R.id.button3).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button3:
                helloTv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mViewModel.setHello();
                    }
                },1000);
//                mViewModel.setHello();
                break;
        }
    }
}
